#include"iostream"
#include"algorithm"

using namespace std;

const int N = 1000010;

int a[N], q[N], n, k;

int main()
{	
	scanf("%d%d", &n,&k);
	for(int i = 0; i < n; i++) scanf("%d", &a[i]);
	
	int head = 0, tail = -1;
	for(int i = 0; i < n; i++)
	{
		if(head <= tail && q[head] < i - k + 1) head++;
		
		while(head <= tail && a[q[tail]] >= a[i]) tail--;
		
		q[++tail] = i;
		if(i >= k - 1) printf("%d ", a[q[head]]);
	}
	puts("");
	head = 0, tail = -1;
	for(int i = 0; i < n; i++)
	{
		if(head <= tail && q[head] < i - k + 1) head++;
		
		while(head <= tail && a[q[tail]] <= a[i]) tail--;
		
		q[++tail] = i;
		if(i >= k - 1) printf("%d ", a[q[head]]);
	}
}